#ifndef OPNAME
#define OPNAME

#include "VMPFocus.hpp"
#include "operations.hpp"

cText TEXT_NAME="name";

//	Operation Name:

//Formula:
//\b[a-zA-Z_]+[0-9_]+\b

//preLining:

//PostLining:

void funOpNamePostL(ListIdentity* what){
	int i=0;
	for (; i<lsOperations.length(); ++i)
		if (lsOperations[i]==Operations[TEXT_NAME])
			break;
	lsOperations.erase(lsOperations.begin()+i);
};

//MkTree:
ptrParsedElement funOpNameMkTree(ptrText code, ptrIdentity identity, ptrParsedElement PE){
	PE->Extended=(void*)(new cText(*code,identity->Range.begin,identity->Range.end-identity->Range.begin+1));
	return PE;
};

//Evaluate:
struct Undefined{
	ptrDough   which;
	ptrRETNode retN;
	ptrText    name;
	Undefined(const ptrDough& x=&PROPHET, const ptrRETNode& t=&GREAT, const ptrText& n=0):which(x),retN(t),name(n){};
};

sRecord funOpNameEva(ptrRETNode retnode, ptrParsedElement pe, void* scope){
	//information:that.
	//Search Method: from this local to this statics, then global. 
	//               if scope is given, look up scope statics.
	sRecord* Result;
	if (scope){
		Result=((ptrDough)scope)->Statics->search(*(ptrText)pe->Extended);
		if (Result) return *Result;
		throw Undefined((ptrDough)scope,retnode,(ptrText)pe->Extended);
	};
	//is no scope given.
	//search local: (this and that are local variables).
	Result=retnode->Locals.search(*(ptrText)pe->Extended);
	if (Result) return *Result;
	//search this statics:
	Result=retnode->Dough->Statics->search(*(ptrText)pe->Extended);
	if (Result) return *Result;
	//search Governmental variables.
	Result=GREAT.Locals.search(*(ptrText)pe->Extended);
	if (Result) return *Result;
	throw Undefined((ptrDough)scope,retnode,(ptrText)pe->Extended);
};

ptrOperation loadOpName(){
	ptrOperation tmp=new cOperation();
	tmp->Name="name";
	tmp->Formula="\\b([a-z]|[A-Z]|_)+[0-9_]*\\b";
	tmp->onEvaluate=funOpNameEva;
	tmp->onMakingTree=funOpNameMkTree;
	tmp->onPostLining=funOpNamePostL;
	tmp->onPreLining=NULL;
	return tmp;
}

#endif